package com.yyge.controller.admin.sysdictdata;

import com.yyge.common.controller.BaseController;
import com.yyge.common.enums.AckCode;
import com.yyge.common.vo.req.PageReq;
import com.yyge.common.vo.resp.PageResp;
import com.yyge.controller.admin.sysdictdata.req.SysDictDataAddReq;
import com.yyge.controller.admin.sysdictdata.req.SysDictDataReq;
import com.yyge.controller.admin.mapstruct.SysDictDataConverter;
import com.yyge.controller.admin.sysdictdata.resp.SysDictDataResp;
import com.yyge.entity.SysDictData;
import com.yyge.service.SysDictDataService;
import com.yyge.common.vo.resp.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyge.util.DicUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.crypto.spec.PSource;
import javax.validation.Valid;
import java.util.*;

/**
 * @Desc 数据字典明细
 * @Author admin
 * @Data 2023/07/03
 */

@Api(tags = "管理后台 - 数据字典明细")
@RestController
@RequestMapping("/dictdata")
public class SysDictDataController extends BaseController {
    // 注入业务逻辑层
    private SysDictDataService service;

    @Autowired/*自动装配*/
    public void setService(SysDictDataService sysdictdataService){
        this.service=sysdictdataService;
    }

    /**
     * Ridis 缓存字典
     */
    @Autowired
    private DicUtil dicUtil;

    @ApiOperation(value = "根据dict_type查询redis缓存中的值")
    @GetMapping("bynames")/* GET(SELECT): 查询 */
    public R<Map<String,List<SysDictDataResp>>> get(PageReq<SysDictData> vo, String[] dicts) throws Exception  {
        if(Objects.isNull(dicts) || dicts.length == 0){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }

        Map<String,List<SysDictDataResp>> dataMap=new HashMap<>();
        for(String key: dicts){
            List<SysDictData> byDictType=this.dicUtil.findByDictType(key);

            //有缓存
            if(Objects.nonNull(byDictType)){
                dataMap.put(key,SysDictDataConverter.MAPPER.sysdictdata2resp(byDictType));
            }
        }
         // 将对象进行转换过滤无效条件
//        SysDictData sysdictdata = SysDictDataConverter.MAPPER.sysdictdata2req(req);
//        QueryWrapper<SysDictData> warpper = new QueryWrapper<>();
//        QueryWrapper queryWrapper = this.getWrapper(sysdictdata,warpper);
//        PageResp pageResp =this.page(vo.getPage(),vo.getSize(),pageInfo ->service.page(pageInfo,queryWrapper));
        return R.okHasData(dataMap);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("data")/* GET(SELECT): 查询 */
    public R<PageResp<SysDictDataResp>> search(PageReq<SysDictData> vo,@Valid SysDictDataReq  req) throws Exception  {
         List<SysDictData> list=this.service.search(vo,req);

        PageResp<SysDictDataResp> resp=new PageResp<>();
        resp.setTotal(vo.getTotal());
        resp.setPageSize(vo.getSize());
        resp.setPageNum(vo.getPage());

        resp.setRows(SysDictDataConverter.MAPPER.sysdictdata2resp(list));
        return R.okHasData(resp);
    }

    @ApiOperation(value = "根据主键查找")
    @GetMapping("{id}")
    public R seeking(@PathVariable String id){
        SysDictData id1 = service.getById(id);
        SysDictDataResp resp = SysDictDataConverter.MAPPER.sysdictdata2resp(id1);
        return R.okHasData(resp);
    }

    @PostMapping/* POST(CREATE): 新增 */
    public R post(@RequestBody SysDictDataAddReq req){
        System.out.println(req);
        SysDictData data = SysDictDataConverter.MAPPER.sysdictdata2req(req);
        System.out.println(data);
        Boolean i=service.save(data);
        this.dicUtil.removeAllRedisDictData();
        this.dicUtil.loadAllDictDataToRedis();
        return i?R.ok("新增成功！"):R.error(AckCode.SYSTEM_SERVICE_ADD_ERROR);
    }

    @PutMapping/* POST(CREATE): 更新 */
    public R out(@RequestBody SysDictData sysdictdata){
        Boolean i=service.updateById(sysdictdata);

        System.out.println("正在刷新缓存.......");
        this.dicUtil.removeAllRedisDictData();
        this.dicUtil.loadAllDictDataToRedis();
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_MODIFY_ERROR);
    }

    @PatchMapping/* PUT(UPDATE): 部分更新 */
    public R patch(@RequestBody SysDictData sysdictdata){
        return null;
    }

    @ApiOperation(value = "逻辑删除")
    @DeleteMapping("/{id}")/* DELETE(DELETE): 删除&&批量删除 */
    public R delete(@PathVariable("id") Integer[] list){
        boolean i = service.removeByIds(Arrays.asList(list));
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_DELETE_ERROR);
    }


}

